package com.zy.explore.sys.interfaces.facade.v1;

import com.zy.explore.common.infrastructure.util.ExportExcelUtil;
import com.zy.explore.sys.application.service.DingTalkService;
import com.zy.explore.sys.infrastructure.constant.SysModuleConstant;
import com.zy.explore.sys.interfaces.frm.AttendDownloadFrm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * description 考勤控制器
 *
 * @author ChinaYin  主页:https://gitee.com/zy-explorej2ee 邮箱:zyexplorej2ee@sina.com
 * @date 2021年04月04日 13:17
 **/
@Api(tags = "考勤管理")
@Slf4j
@RestController
@RequestMapping("/attend")
public class AttendanceFacade {

    @Resource
    private DingTalkService dingTalkService;

    @ApiOperation("导出指定部门指定月份的加班考勤")
    @PostMapping("/downloadOvertimeAttend")
    public void downloadOvertimeAttend(@RequestBody @Valid AttendDownloadFrm frm, HttpServletResponse response) throws Exception {
        Integer type = frm.getType();
        String name = frm.getName();
        String id = frm.getId();
        String year = frm.getYear();
        String month = frm.getMonth();
        log.info("下载加班考勤入参：type={}， name={}， id={}，year={}，month={}", type, name, id, year, month);
        //String title = deptName + year + "年" + month + "月餐补统计";
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        //response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(title, "UTF-8") + ".xlsx");
        response.setHeader("Content-Disposition", "attachment; filename=data.xlsx");
        response.setHeader("Connection", "close");
        OutputStream outputStream = response.getOutputStream();
        ExportExcelUtil eeu = new ExportExcelUtil();
        XSSFWorkbook workbook = new XSSFWorkbook();
        String[] header = {"姓名", "日期", "具体时间", "金额(元)", "费用类型"};
        if (type == SysModuleConstant.DEPT) {
//            Map<String, List<List<String>>> map = dingTalkService.getDeptOverTimeAttendListMap(name, Long.parseLong(id), Integer.parseInt(year), Integer.parseInt(month));
//            int i = 1;
//            for (String emp : map.keySet()) {
//                // 各员工的明细
//                eeu.exportExcel(workbook, i++, emp, header, map.get(emp));
//            }
            List<List<String>> all = dingTalkService.getDeptOverTimeAttendList(name, Long.parseLong(id), Integer.parseInt(year), Integer.parseInt(month));
            eeu.exportExcel(workbook, 0, name + year + "年" + month + "月份-餐补汇总", header, all);
        } else if (type == SysModuleConstant.PERSONNEL) {
            Map<String, List<List<String>>> map = dingTalkService.getEmpOverTimeAttendList(name, id, Integer.parseInt(year), Integer.parseInt(month));
            int i = 0;
            for (String emp : map.keySet()) {
                eeu.exportExcel(workbook, i++, emp, header, map.get(emp));
            }
        }
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }

}
